// Created by WXX on 2021/10/22 16:32
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：4 ms, 在所有 C++ 提交中击败了93.90%的用户
 * 内存消耗：8.6 MB, 在所有 C++ 提交中击败了72.13%的用户
 */
class Solution {
public:

    vector<vector<int>> ans;
    vector<int> path;
    vector<bool> st;

    vector<vector<int>> permuteUnique(vector<int>& nums) {

        sort(nums.begin(), nums.end());

        int n = nums.size();
        path = vector<int>(n);
        st = vector<bool>(n);
        dfs(nums, 0);
        return ans;
    }

    void dfs(vector<int> &nums, int u) {

        if (u == nums.size()) {
            ans.push_back(path);
            return;
        }

        for (int i = 0; i < nums.size(); i++) {
            if (!st[i]) {
                if (i && nums[i] == nums[i - 1] && !st[i - 1]) continue;
                st[i] = true;
                path[u] = nums[i];
                dfs(nums, u + 1);
                st[i] = false;
            }
        }
    }
};

void OutputBasicArray2D1(vector<vector<int>> nums) {

    cout << "[";
    for (int i = 0; i < nums.size(); i++) {
        cout << "[";
        for (int j = 0; j < nums[i].size(); j++) {
            cout << nums[i][j];
            if (j != nums[i].size() - 1) cout << ", ";
        }
        cout << "]";
        if (i != nums.size() - 1) cout << ", ";
    }
    cout << "]" << endl;
}

int main() {

    vector<int> nums = {1, 1, 2};
    OutputBasicArray2D1(Solution().permuteUnique(nums));

    return 0;
}
